import csv

# 获取用户输入并存入到列表中
input_str = input("请输入你要查询的城市和项目以逗号分隔：\n")
input_items = [item.strip() for item in input_str.split(",")]
# 将城市和项目分出来
city = input_items[0]
categories = input_items[1:]
print(f"你输入的城市是：{city}")
# 读取CSV文件
with open("xiaofei.csv", "r") as f:
    reader = csv.reader(f)
    # 读取表头
    header = next(reader)
    # 去除表头空格
    header = [col.strip() for col in header]
    # 获取项目对应的列索引
    col_indices = []
    for category in categories:
        idx = header.index(category)
        col_indices.append(idx)
    # 查找城市数据
    for row in reader:
        if row[0].strip() == city:
            for i, category in enumerate(categories):
                # 确保有对应的列索引
                if i < len(col_indices):
                    col = col_indices[i]
                    # 确保列索引有效
                    if col < len(row):
                        print(f"{category}的消费金额为：{row[col]}")
                    else:
                        print(f"{category}的数据缺失")
            break